<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增主机</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<style>
    .layui-form-item{
        width: 50%;
    }
    label{
        width:95px !important;
    }
    .layui-input-block{
        margin-left:125px;
    }
</style>
<body>
<div style="width:80%;margin:30px auto;" id="aaa">
    <form class="layui-form" action="">
        <div style="">
<!--            <input type="hidden" class="input-text" id="cl_paizhao" name="cl_paizhao">-->
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>主机别名</label>
                <div class="layui-input-block">
                    <input type="text" name="remarks" placeholder="请输入主机别名" lay-verify="required|zhongwen" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span> 主机地址</label>
                <div class="layui-input-block">
                    <input type="text" name="address"  lay-verify="required" placeholder="请输入主机地址" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>主机端口</label>
                <div class="layui-input-block">
                    <input type="text" name="port" placeholder="请输入主机端口" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span> 用户ID</label>
                <div class="layui-input-block">
                    <input type="text" name="uid"  placeholder="请输入用户ID" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label"><span style="color: red">*</span> 伪装类型</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <select name="type" lay-verify="required" lay-filter="type" id="type" lay-search>-->
<!--                        <option value="" selected>选择伪装类型</option>-->
<!--                        <option value="0">国内通国外</option>-->
<!--                        <option value="1">国外通国内</option>-->
<!--                        <option value="2">国内外互通</option>-->
<!--                    </select>-->
<!--                </div>-->
<!--            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>最大连接数</label>
                <div class="layui-input-block">
                    <input type="number" name="lianjie_num" oninput="value=value.replace(/[^\d]/g,'')" lay-verify="required|double|number" placeholder="请输入最大连接数" autocomplete="off" class="layui-input">
                </div>
            </div>
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label">到期时间</label>-->
<!--                <div class="layui-input-block">-->
<!--                  <input type="text" id="end_time" lay-verify="datetime" name="end_time" autocomplete="off" placeholder="请输入到期时间" class="layui-input">-->
<!--                </div>-->
<!--            </div>-->

            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span> 额外ID</label>
                <div class="layui-input-block">
                    <input type="text" name="out_id" lay-verify="required" placeholder="请输入额外ID" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"> 版本号</label>
                <div class="layui-input-block">
                    <input type="text" name="version" placeholder="请输入版本号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">连接方式</label>
                <div class="layui-input-block">
                    <input type="text" name="net"  placeholder="请输入连接方式" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">伪装类型</label>
                <div class="layui-input-block">
                    <input type="text" name="wz_type"  placeholder="请输入伪装类型" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color:red">*</span>所属区域</label>
                <div class="layui-input-inline" style="width: 220px">
                    <select lay-verify="required" lay-filter="country_id" lay-search id="country_id" name="country_id">
                        <option value="">选择所属国家</option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width: 220px">
                    <select lay-verify="required" lay-search id="area_id" name="area_id">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button style="width: 100px;" class="layui-btn" id="addZhuji" lay-submit lay-filter="addZhuji">确定</button>
                <button style="width: 100px;" type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }
    var form = layui.form;
    var json = GetQueryString("basema");
    if(json=="" || json=="undefined"){
        layer.msg("您没有转base码！");
    }
    var basema = JSON.parse(json);
    window.onload = function(){
        if(basema){
            $("input[name='remarks']").val(basema.ps);
            $("input[name='address']").val(basema.add);
            $("input[name='port']").val(basema.port);
            $("input[name='uid']").val(basema.id);
            $("input[name='out_id']").val(basema.aid);
            $("input[name='version']").val(basema.v);
            $("input[name='net']").val(basema.net);
            $("input[name='wz_type']").val(basema.type);
        }
    }
    layui.use(['form' , 'jquery' , 'upload',], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        //验证是不是数字
        form.verify({
            double: function(value){
                if(/^\d+$/.test(value)==false && /^\d+\.\d+$/.test(value)==false)
                {
                    return '你输入的不是数字';
                }
            },
            datetime: function (value) {
                if(value != ""){
                    if( /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/.test(value)==false)
                    {
                        return '日期格式不正确';
                    }
                }
            },
            zhongwen: function (value) {
                if(value != ""){
                    if (/[\u4E00-\u9FA5]/g.test(value)) {
                        return '不能输入中文名';
                    }
                }
            }
        });
        $.ajax({
            url:"/app/app_guojia/selectCountrys",
            success:function(res){
                if(res.code == 0) {
                    for(var i in res.data) {
                        $("#country_id").append("<option value='"+res.data[i].id+"'>"+res.data[i].country_zh_name+"/"+res.data[i].country_en_name+"</option>")
                    }
                    form.render()
                }
            }
        })
        form.on('select(country_id)', function(data){
            var country_id = data.value;
            if($("#country_id").val()){
                $("#area_id").empty();
                $.ajax({
                    url: "/guojia/selectAreasBycountryID"
                    , type: "POST"
                    , data: {'country_id':country_id}
                    , success: function (res) {
                        if(res.code == 0) {
                            for(var i in res.data) {
                                $("#area_id").append("<option value='"+res.data[i].id+"'>"+res.data[i].area_zh_name+"/"+res.data[i].area_en_name+"</option>")
                            }
                            form.render()
                        }
                    }
                });
            }else {
                $("#area_id").html("");
                form.render();
            }
            return false;
        });
        //编辑操作
        form.on('submit(addZhuji)',function(data){
            $.ajax({
                url: "/zhuji/addZhuji"
                , type: "POST"
                , contentType: 'application/json'
                , data: JSON.stringify(data.field)
                , success: function (res) {
                    // console.log(res);
                    if (res.code == 0) {
                        layer.msg(res.msg);
                        setTimeout(function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            window.location.reload();
                        }, 2000);
                    } else if (res.code == 1) {
                        layer.msg(res.msg);
                    } else {
                        layer.msg('拒绝访问');
                    }
                }
                , error: function () {
                    console.log("ajax error");
                }
            });
            return false;
        });

    });

</script>
<script>

    layui.use('laydate', function(){
        var laydate = layui.laydate;
        //日期范围
       laydate.render({
            elem: '#end_time',
            type: 'datetime',
        });
    });
</script>
</body>
</html>